package com.tjtt.tdm.system.service.impl;



import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.LogInfo;
import com.tjtt.tdm.system.dao.LogDao;
import com.tjtt.tdm.system.service.LogService;

public class LogServiceImpl implements LogService {
	
	private static final Log log = LogFactory.getLog(LogServiceImpl.class);
	private LogDao LogDao;

	
	public void saveLogInfo(LogInfo logInfo,String editModule, String logType, String logEvent) throws ServiceException {
		
		try{
			logInfo.setLogLoginEvent(logEvent);
			logInfo.setLogType(logType);
			logInfo.setLogModuleName(editModule);
			this.LogDao.save(logInfo);
		}catch(Exception e){
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

public void saveLogInfo(LogInfo logInfo,String editModule, String logType, String logEvent,Integer IimitsID) throws ServiceException {
		
		try{
			logInfo.setLogLoginEvent(logEvent);
			logInfo.setLogType(logType);
			logInfo.setLogModuleName(editModule);
			logInfo.setIimitsID(IimitsID);
			if(!"4".equals(logInfo.getLogUserId())){
				this.LogDao.save(logInfo);
			}
		}catch(Exception e){
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public void saveLogError(LogInfo logInfo, String logType, String logEvent,
			String editModule, Exception e) throws ServiceException {
		try{
			logInfo.setLogLoginEvent(logEvent);
			logInfo.setLogType(logType);
			logInfo.setLogModuleName(editModule);
			StackTraceElement[] st = e.getStackTrace();
			String str = e.toString()+"\n";
			str += "错误类信息 \n";
			str += "FileName\tClassName\t\tMethodName\tLineNumber\n";
			for (int i = 0; i < st.length; i++) {
				String methodName = st[i].getClassName();
				if(null!= methodName && methodName.indexOf("com.tjtt.tdm")==0){
					str += st[i].getFileName() + "\t" + st[i].getClassName() + "\t" + st[i].getMethodName() + "\t\t" + st[i].getLineNumber() + "\n";
				}
			}
			logInfo.setLogErrorInfo(str);
			this.LogDao.save(logInfo);
		}catch(Exception error){
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
		
	}


	public LogInfo findLogById(long logId) {
    	return this.LogDao.getLogById(logId);
	}

	public PageResult getLogList(Map filterMap) {
		return this.LogDao.queryLogList(filterMap);
	}
	
	public LogDao getLogDao() {
		return LogDao;
	}

	public void setLogDao(LogDao logDao) {
		LogDao = logDao;
	}

 

	
	public void delLogInfo(int sysLogsaveDay) throws ServiceException {
		try {
			this.LogDao.delLogInfo(sysLogsaveDay);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	   /**
     * 清除所有日志
     * author:lishu	 2015-5-27
     */
    public void deleteLogAll(){
    	LogDao.deleteLogAll();
    }
}
